Analytics Usage Guide
Tracking an Event
To track an event, define it by implementing AnalyticsEvent, then pass it to the tracker.
Optionally, define context properties you want to be collected and sent with the event.
kotlin1object ButtonId : EventAnalyticsProperty("button_id")23data class ButtonClick(4 val buttonId: String,5 override val createdAt: Instant = Instant.nowUtc(),6) : AnalyticsEvent {78 override val name = "button_click"910 override val eventProperties: Map<EventAnalyticsProperty, String> = mapOf(11 ButtonId to buttonId12 )1314 override val contextProperties: Set<ContextAnalyticsProperty> = setOf(15 ContextAnalyticsProperty.AppVersion,16 ContextAnalyticsProperty.Platform,17 ContextAnalyticsProperty.CurrentPage,18 )19}2021analyticsTracker.track(ButtonClick("submit"))
Collect Context Properties
Collect properties that should attach to future events. Define custom keys before using them:
kotlin1object UserId : ContextAnalyticsProperty("user_id", AnalyticsPropertyScope.USER)
An event must include UserId in its contextProperties set to receive that value.
kotlin1// Set global properties (e.g., on app start)2analyticsTracker.track(3 ContextAnalyticsProperty.AppVersion to "1.0.0",4 ContextAnalyticsProperty.Platform to "Android"5)67// Set user properties (e.g., on login)8analyticsTracker.track(9 UserId to "user_12345"10)
Clearing Context Properties
Clear properties when they are no longer valid to prevent data pollution.
kotlin1// Clear specific user properties on logout2analyticsTracker.clear(AnalyticsPropertyScope.USER)
Context Ordering
Submit context before events that need the updated value. The pipeline processes accepted commands in queue order.
Navigation submits its event before updating CurrentPage, so PageName identifies the destination while CurrentPage contains the preceding value.